import java.util.Arrays;
import java.util.Stack;

//数组实现栈
public class MyStack {

    public int[] elem;
    public int usedSize;
    public static final int DEFAULT_SIZE = 10;

    public MyStack() {
        this.elem = new int[DEFAULT_SIZE];
    }

    /**
     * 弹出
     *
     * @return
     */
    public int pop() {
        if (empty()) {
            throw new Myemptyexption("栈为空!");
        }
        return this.elem[--usedSize];
    }

    public boolean empty() {
        if (this.usedSize == 0) {
            return true;
        }
        return false;
    }

    /**
     * 压栈
     *
     * @return
     */
    public int push(int val) {
        if (!isFull()) {
            this.elem = Arrays.copyOf(this.elem, 2 * elem.length);
        }
        this.elem[this.usedSize] = val;
        this.usedSize++;
        return val;
    }

    public boolean isFull() {
        if (this.usedSize == this.elem.length) {
            return true;
        }
        return false;
    }

    /**
     * @return
     */
    public int peek() {
        if (empty()) {
            throw new Myemptyexption("栈为空!");
        } else {
            return this.elem[usedSize - 1];
        }
    }

    /**
     * 查找元素对应的下标
     * @param val
     * @return
     */
    public int search(int val) {
        for (int i = 0; i < usedSize; i++) {
            if (this.elem[i] == val) {
                return i;
            }
        }
        return -1;
    }

}
